package com.kwan.xframe.mvp.view.activity;

import android.graphics.Bitmap;
import android.os.Build;
import android.util.Log;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.kwan.xframe.R;

import static com.kwan.xframe.R.id.webView;

/**
 * 腾讯tbs webview框架
 * <p>
 * 引用库：
 * libs/tbs_sdk_thirdapp_v2.6.0.1045.jar
 * jniLibs/liblbs.so
 * <p>
 * Appliction 中需要进行相关初始化
 * <p>
 * Created by Mr.Kwan on 2017-1-4.
 */

public abstract class BaseWebActivity extends BaseCommonActivity {

	protected WebView tbsWebView;
	public static final int MODE_WEB = 1;
	public static final int MODE_CONTENT = 2;
	protected String mUrl;

	@Override
	protected void beForeSetContentView() {
		super.beForeSetContentView();
		mUrl = getUrl();
	}

	/**
	 * @return 可以是地址 也可以是 html 内容
	 */

	protected abstract String getUrl();

	@Override
	protected Bitmap getBackGroundBitmap() {
		return null;
	}

	@Override
	protected int getMainViewId() {
		return R.layout.activity_base_web;
	}

	@Override
	protected void initViews() {
		super.initViews();
		tbsWebView = findViewById(webView);
	}

	@Override
	protected void initData() {

		showProgress("");
		if (getMode() == MODE_WEB)
			loadWeb(mUrl);
		else
			loadContent(mUrl);
	}

	protected abstract int getMode();

	protected void loadWeb(final String url) {

		//final String url = "http://m.zufang.com";

		tbsWebView = findViewById(webView);
		tbsWebView.loadUrl(url);

		WebSettings settings = tbsWebView.getSettings();
		settings.setJavaScriptEnabled(true);

		//https与http混合资源处理
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
			settings.setMixedContentMode(android.webkit.WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
		}


		// 设置加载进来的页面自适应手机屏幕
		settings.setUseWideViewPort(true);
		settings.setLoadWithOverviewMode(true);

		settings.setSupportZoom(true);
		settings.setBuiltInZoomControls(true);
		settings.setDisplayZoomControls(true);

		//settings.setUserAgent( );

		tbsWebView.setWebViewClient(new WebViewClient() {
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				Log.e("WebView ", "shouldOverrideUrlLoading " + url);
				if (url.startsWith("http:") || url.startsWith("https:")) {
					view.loadUrl(url);
					return false;
				} else {
//                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
//                startActivity(intent);
					return true;
				}
			}

			@Override
			public void onPageFinished(WebView webView, String s) {
				super.onPageFinished(webView, s);
				dismissProgress();
			}
		});

		tbsWebView.setWebChromeClient(new WebChromeClient() {
			@Override
			public void onProgressChanged(WebView view, int newProgress) {
				super.onProgressChanged(view, newProgress);
			}

		});

	}



	protected void loadContent(final String content) {

//
//		TextView txt = (TextView)findViewById(R.id.txt);
//		txt.setText(content);
//
//		Logger.e(content);


		WebSettings mSetting = tbsWebView.getSettings();
		mSetting.setJavaScriptEnabled(true);
		mSetting.setDefaultTextEncodingName("UTF-8");
		mSetting.setCacheMode(WebSettings.LOAD_DEFAULT);

//        webview.addJavascriptInterface(new ProductsInfoActivityJSService(
//                this),"Javahelper");   // 这在前

		tbsWebView.setWebViewClient(new WebViewClient() {

			@Override
			public boolean shouldOverrideUrlLoading(WebView webView, String s) {
				if (s.startsWith("http:") || s.startsWith("https:")) {
					webView.loadUrl(s);
					return false;
				} else {
//                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
//                startActivity(intent);
					return true;
				}
			}

			@Override
			public void onPageFinished(WebView view, String url) {
				super.onPageFinished(view, url);
				dismissProgress();
			}
		});
		tbsWebView.postDelayed(new Runnable() {
			@Override
			public void run() {
				if (tbsWebView != null)
					tbsWebView.loadDataWithBaseURL("", content, "text/html", "UTF-8", "");
			}
		}, 1000);

	}

	@Override
	public void onBackPressed() {
		if (tbsWebView.canGoBack())
			tbsWebView.goBack();
		else
			super.onBackPressed();
	}
}
